//package com.equipment.management.mqtt;
//
//import com.equipment.management.entity.Mainframe;
//import com.equipment.management.service.MainframeService;
//import lombok.extern.slf4j.Slf4j;
//import org.eclipse.paho.client.mqttv3.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//import java.util.Date;
//import java.util.List;
//
//@Slf4j
//public class CallBack implements MqttCallback {
//
//    private MqttClient client;
//    private MqttConnectOptions options;
//
//
//    @Value("${spring.mqtt.username}")
//    private String username;
//
//    @Value("${spring.mqtt.password}")
//    private String password;
//
//    @Value("${spring.mqtt.url}")
//    private String hostUrl;
//
//    @Value("${spring.mqtt.client.id}")
//    private String clientId;
//
//    @Value("${spring.mqtt.default.topic}")
//    private String defaultTopic;
//
//    @Value("${spring.mqtt.completionTimeout}")
//    private int completionTimeout ;   //连接超时
//
//
//    @Bean
//    public MqttConnectOptions getMqttConnectOptions() {
//        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
//        mqttConnectOptions.setUserName(username);
//        mqttConnectOptions.setPassword(password.toCharArray());
//        mqttConnectOptions.setServerURIs(new String[]{hostUrl});
//        mqttConnectOptions.setKeepAliveInterval(2);
//        mqttConnectOptions.setCleanSession(false);
//        mqttConnectOptions.setConnectionTimeout(10);
//        mqttConnectOptions.setKeepAliveInterval(60);
//        mqttConnectOptions.setAutomaticReconnect(true);
//        return mqttConnectOptions;
//    }
//    @Autowired
//    private MainframeService mainframeService;
//    public CallBack() {
//    }
//
//    public CallBack(MqttClient client, MqttConnectOptions options) {
//        log.info("client:{}",client);
//        log.info("options:{}",options);
//
//        this.client = client;
//        this.options = options;
//
//    }
//    //方法实现说明 断线重连方法，如果是持久订阅，重连是不需要再次订阅，如果是非持久订阅，重连是需要重新订阅主题 取决于options.setCleanSession(true); true为非持久订阅
//    @Override
//    public void connectionLost(Throwable cause) {
//        //失败重连逻辑
//        while (true){
//            try {
//                System.out.println("连接失败重连");
//                client.connect(options);
//                //发布相关的订阅
//                List<Mainframe> list=mainframeService.getAllmainframe();
//                    List<String>main=new ArrayList<>();
//                    List<Integer>qoslist=new ArrayList<>();
//                for(Mainframe mainframe:list){
//                    main.add(mainframe.getCode());
//                    qoslist.add(1);
//                }
//
//
//                String[] topic = main.toArray(new String[list.size()]);
//                Integer[] qos =qoslist.toArray(new Integer[list.size()]);
//                int[] qosint=Arrays.stream(qos).mapToInt(Integer::valueOf).toArray();
//                client.subscribe(topic, qosint);
//                System.out.println("连接失败重连成功");
//                break;
//            } catch (MqttException e) {
//                e.printStackTrace();
//                System.out.println("连接失败重连失败");
//            }
//        }
//    }
//
//    @Override
//    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
//        System.out.println(new Date());
//        System.out.println("public void messageArrived(String s, MqttMessage mqttMessage)");
//        System.out.println("666"+s);
//        System.out.println(new String(mqttMessage.getPayload(),"UTF-8"));
//    }
//
//    @Override
//    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
//        System.out.println("public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken)");
//    }
//}